/**
 * 滚动到页面顶部
 */
export const scrollToTop = () => {
  window.scrollTo({ top: 0, left: 0, behavior: 'smooth' })
}

/**
 * 滚动到页面底部
 */
export const scrollToBottom = () => {
  window.scrollTo({
    top: document.documentElement.offsetHeight,
    left: 0,
    behavior: 'smooth'
  })
}

/**
 * 将元素滚动到可见区域
 *
 */
export const smoothScroll = (element) => {
  element.scrollIntoView({
    behavior: 'smooth'
  })
}

/**
 * 获取数据类型
 */
export const getType = (value) => {
  const match = Object.prototype.toString.call(value).match(/ (\w+)]/)
  return match[1].toLocaleLowerCase()
}

/**
 * 深拷贝
 */
export const deepCopy = (obj, hash = new WeakMap()) => {
  if (obj instanceof Date) {
    return new Date(obj)
  }
  if (obj instanceof RegExp) {
    return new RegExp(obj)
  }
  if (hash.has(obj)) {
    return hash.get(obj)
  }
  const allDesc = Object.getOwnPropertyDescriptors(obj)
  const cloneObj = Object.create(Object.getPrototypeOf(obj), allDesc)
  hash.set(obj, cloneObj)
  for (const key of Reflect.ownKeys(obj)) {
    if (obj[key] && typeof obj[key] === 'object') {
      cloneObj[key] = deepCopy(obj[key], hash)
    } else {
      cloneObj[key] = obj[key]
    }
  }
  return cloneObj
}

/**
 * 判断设备类型
 */
export const isMobile = () => {
  return !!navigator.userAgent.match(
    /(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symbian|Windows Phone|Phone)/i
  )
}

/**
 * 判断iOS、Android
 */
export const isAndroid = () => {
  return /android/i.test(navigator.userAgent.toLowerCase())
}

export const isIOS = () => {
  const reg = /iPhone|iPad|iPod|iOS|Macintosh/i
  return reg.test(navigator.userAgent.toLowerCase())
}

/**
 * 获取浏览器类型和版本
 */
export const getExplorerInfo = () => {
  const t = navigator.userAgent.toLowerCase()
  return t.indexOf('msie') >= 0
    ? {
      // ie < 11
        type: 'IE',
        version: Number(t.match(/msie ([\d]+)/)[1])
      }
    : t.match(/trident\/.+?rv:(([\d.]+))/)
      ? {
        // ie 11
          type: 'IE',
          version: 11
        }
      : t.indexOf('edge') >= 0
        ? {
            type: 'Edge',
            version: Number(t.match(/edge\/([\d]+)/)[1])
          }
        : t.indexOf('firefox') >= 0
          ? {
              type: 'Firefox',
              version: Number(t.match(/firefox\/([\d]+)/)[1])
            }
          : t.indexOf('chrome') >= 0
            ? {
                type: 'Chrome',
                version: Number(t.match(/chrome\/([\d]+)/)[1])
              }
            : t.indexOf('opera') >= 0
              ? {
                  type: 'Opera',
                  version: Number(t.match(/opera.([\d]+)/)[1])
                }
              : t.indexOf('Safari') >= 0
                ? {
                    type: 'Safari',
                    version: Number(t.match(/version\/([\d]+)/)[1])
                  }
                : {
                    type: t,
                    version: -1
                  }
}

/**
 * 设置cookies
 */
export const setCookie = (key, value, expire) => {
  const d = new Date()
  d.setDate(d.getDate() + expire)
  document.cookie = `${key}=${value};expires=${d.toUTCString()}`
}

/**
 * 获取cookies
 */
export const getCookie = (key) => {
  const cookieStr = decodeURI(document.cookie)
  const arr = cookieStr.split('; ')
  let cookieValue = ''
  for (let i = 0; i < arr.length; i++) {
    const temp = arr[i].split('=')
    if (temp[0] === key) {
      cookieValue = temp[1]
      break
    }
  }
  return cookieValue
}

/**
 * 删除cookies
 */

export const delCookie = (key) => {
  document.cookie = `${encodeURIComponent(key)}=;expires=${new Date()}`
}

/**
 * 首字母大写
 */
export const fistLetterUpper = (str) => {
  return str.charAt(0).toUpperCase() + str.slice(1)
}

/**
 * 生成指定范围内的随机数
 */
export const randomNum = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min
